//表单验证，添加预订和入住登记共用
function formValidate(){
    var inputs = $("#reserve_tab tr td input:not(#address)"); //全部文本框
    //如果为9个文本框表示来到入住登记页面，将预订时间文本框排除
    if (inputs.length == 9){
        inputs = $("#reserve_tab tr td input:not(#address):not(#reservedDate)");
    }
    var select = $("#reserve_tab tr td select");//全部下拉框
    var $idNumber = $("#idNumber"); //身份证
    var $phone = $("#phone"); //手机号
    var $reservedDate = $("#reservedDate");//预订时间
    var $checkOutDate = $("#checkOutDate");//离店时间
    var $checkInDate = $("#checkInDate");//入住时间
    var validateResult = 0 //验证是否全部填写除地址外
    //当格式不对修改错误信息，当为空需要把错误信息修改回来
    $idNumber.next().css("display","none").html("请输入身份证号");
    $phone.next().css("display","none").html("请输入手机号");
    $checkOutDate.next().css("display","none").html("请输入离店时间");
    $checkInDate.next().css("display","none").html("请输入入住时间");
    //遍历判断文本框是否为空,并提示相关信息
    $.each(inputs,function(index,inp){
        if ($(inp).val() == "" && $(inp).val().length == 0){
            $(inp).css("border","solid 1px red");
            $(inp).next().css("display","inline-block");
            validateResult++;
        }else {
            $(inp).css("border","solid 1px green");
            $(inp).next().css("display","none");
        }
    });
    //遍历判断下拉框是否为空,并提示相关信息
    $.each(select,function(index,sel){
        if ($(sel).val() == -1){
            $(sel).css("border","solid 1px red");
            $(sel).next().css("display","inline-block");
            validateResult++;
        }else {
            $(sel).css("border","solid 1px green");
            if (index == 0){
                $(sel).next().next().next().css("display","none");
            }else{
                $(sel).next().css("display","none");
            }
        }
    });

    //当全部没填就不执行下面的格式验证
    if (inputs.length == 8) if(validateResult == 10) return;
    //验证身份证号、手机号、预、入住时间格式是否非法
    //当身份证号不为空验证格式是否正确
    if (!($idNumber.val() == "" && $idNumber.val().length == 0)){
        var idNumberValidate = /^\d{18}$/;
        if (!idNumberValidate.test($idNumber.val())){
            $idNumber.next().css("display","inline-block").html("身份证号必须是18位");
            $idNumber.css("border","1px solid red");
            validateResult++;
        }else{
            $idNumber.css("border","1px solid green");
        }
    }
    //当手机号不为空验证格式是否正确
    if (!($phone.val() == "" && $phone.val().length == 0)){
        var phoneValidate = /^\d{11}$/;
        if (!phoneValidate.test($phone.val())){
            $phone.next().css("display","inline-block").html("手机号必须是11位");
            $phone.css("border","1px solid red");
            validateResult++;
        }else{
            $phone.css("border","1px solid green");
        }
    }
    //当离店时间和预订时间不为空验证是否小于预订时间
    if (!($checkOutDate.val() == "" && $checkOutDate.val().length == 0 && $reservedDate.val() == "" && $reservedDate.val().length == 0)){
        var checkOutDate = new Date($checkOutDate.val());
        var reservedDate = new Date($reservedDate.val());
        if (checkOutDate < reservedDate){
            $checkOutDate.next().css("display","inline-block").html("不能小于预订时间");
            $checkOutDate.css("border","1px solid red");
            validateResult++;
        }else{
            $checkOutDate.css("border","1px solid green");
        }
    }
    //入住登记模块的验证
    //当入住时间和离店时间不为空验证是否大于离店时间
    if (!($checkInDate.val() == "" && $checkInDate.val().length == 0 && $checkOutDate.val() == "" && $checkOutDate.val().length == 0)){
        var checkInDate = new Date($checkInDate.val());
        var checkOutDate = new Date($checkOutDate.val());
        if (checkInDate > checkOutDate){
            $checkInDate.next().css("display","inline-block").html("不能大于离店时间");
            $checkInDate.css("border","1px solid red");
            validateResult++;
        }else{
            $checkInDate.css("border","1px solid green");
        }
    }
    //验证结果返回boolean，如果validateResult为0返回true表示全部填写，否则为false
    if (validateResult == 0) {
        //入住登记，如果该客户以预订房间则不验证是否选择房间
        if ($("#checkIn").html() == "入住" && $("#reservedDate").val().length != 0) return true;
        //验证用户是否选择房间
        var $inps = $("#roomTab tbody tr td input");
        var count = 0;
        $.each($inps, function (index, inp) {
            if ($(inp).prop("checked") == true) {
                count++;
            }
        });
        if (count != 1) {
            showModal("温馨提示","您还未安排房间");
            return false;
        }
        return true;
    }
    return false;
}